package edu.arizona.sista.learn.model.displaying;

import java.io.File;

import com.martiansoftware.jsap.FlaggedOption;
import com.martiansoftware.jsap.JSAP;
import com.martiansoftware.jsap.JSAPException;
import com.martiansoftware.jsap.JSAPResult;
import com.martiansoftware.jsap.Switch;

import edu.arizona.sista.learn.model.Interval;
import edu.arizona.sista.learn.model.holding.HoldingAnObject;
import edu.arizona.sista.learn.model.holding.Scene;
import edu.arizona.sista.learn.util.Utils;
import edu.arizona.sista.learn.util.Utils.MovieName;

public class Main {
	public static void main(String[] args) throws JSAPException {
		
		JSAP jsap = new JSAP();
		Switch helpSwitch = new Switch("help")
				.setShortFlag('h')
				.setLongFlag("help");
		FlaggedOption pose = new FlaggedOption("pose-file")
				.setDefault("")
				.setRequired(true)
				.setLongFlag("pose-file");
		FlaggedOption bodyParts = new FlaggedOption("bodyParts-file")
				.setDefault("")
				.setRequired(true)
				.setLongFlag("bodyParts-file");
		FlaggedOption lookat = new FlaggedOption("lookat-file")
				.setDefault("")
				.setRequired(true)
				.setLongFlag("lookat-file");
		FlaggedOption output = new FlaggedOption("output-file")
				.setDefault("")
				.setRequired(true)
				.setLongFlag("output-file");

		jsap.registerParameter(helpSwitch);
		jsap.registerParameter(pose);
		jsap.registerParameter(bodyParts);
		jsap.registerParameter(lookat);
		jsap.registerParameter(output);

		JSAPResult config = jsap.parse(args);
		if (config.getBoolean("help")
				|| "".equals(config.getString("pose-file"))
				|| "".equals(config.getString("bodyParts-file"))
				|| "".equals(config.getString("lookat-file"))
				|| "".equals(config.getString("output-file"))) {
			System.out.println(jsap.getUsage());
			return;
		}
		File poseFile = new File(config.getString("pose-file"));
		if (!poseFile.isFile()) {
			System.err.println("File not found or invalid!");
			return;
		}
		File bodyPartsFile = new File(config.getString("bodyParts-file"));
		if (!bodyPartsFile.isFile()) {
			System.err.println("File not found or invalid!");
			return;
		}
		File lookFile = new File(config.getString("lookat-file"));
		if (!lookFile.isFile()) {
			System.err.println("File not found or invalid!");
			return;
		}
		
		getDisplayedObject(poseFile, bodyPartsFile, lookFile, config.getString("output-file"));
	}

	private static void getDisplayedObject(File poseFile, File bodyPartsFile, File lookFile, String output) {
		
		//Name of the scene
		MovieName sceneName = MovieName.Map;
		if (lookFile.getAbsolutePath().toLowerCase().contains("newspaper")) sceneName = MovieName.Newspaper;
		
		Scene scene = new Scene(sceneName);
		
		//Load data
		scene.loadPositionData(poseFile);
		scene.loadBodyParts(bodyPartsFile);
		
		//Get hold things
		HoldingAnObject.getHoldThings(scene);
		
		//Get watched things
		WatchingSomething.loadLook(lookFile, scene);
		
		//Get displayed things
		DisplayingSomething.getDisplayedThings(scene, Interval.NOISE_WINDOW);
		
		StringBuilder sb = new StringBuilder();
		sb.append(scene.toString());
		Utils.toFile(sb, output);
		
		//System.out.println(scene);
		
	}
}
